package com.itboye.ihomebank.message;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.SystemMessageAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.SystemMessBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.custom.XListView.IXListViewListener;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 系统消息
 * 
 */
public class ActivitySystemMessage extends BaseOtherActivity implements
		Observer, IXListViewListener {
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题
	XListView mess_listview;
	TextView no_mess;

	private UserPresenter userPresenter;

	List<SystemMessBean.Sysitem> list = new ArrayList<SystemMessBean.Sysitem>();
	
	SystemMessageAdapter adapter;

	private int pageNo = 1;
	private String uid;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_system_message;
	}

	@Override
	protected void onInitController() {
		mess_listview.setXListViewListener(this);
		mess_listview.setPullLoadEnable(false);
		mess_listview.setPullRefreshEnable(false);

	}

	@Override
	protected void onInit() {
		add_shap_title_tv.setText("系统消息");

		uid = (String) SPUtils.get(this, null, SPContants.USER_ID, "");

		userPresenter = new UserPresenter(this);
		userPresenter.getSystemMess(uid, "1", pageNo + "", "10");

	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());

		if (entity != null) {
			if (!entity.getCode().equals(0)) {

				if (entity.getEventType() == UserPresenter.sysMess_success) {
					SystemMessBean bean = (SystemMessBean) entity.getData();

					if (pageNo == 1) {// 当页码数为1时，清空，以免下拉时重复加载数据
						list.clear();
					}

					if (adapter == null) {
						adapter = new SystemMessageAdapter(this, list,
								R.layout.item_system_message);
						mess_listview.setAdapter(adapter);
					}
					if (bean.getCount() > 0) {
						mess_listview.setVisibility(View.VISIBLE);
						no_mess.setVisibility(View.GONE);
						list.addAll(bean.getList());
						adapter.notifyDataSetChanged();
					}else{
						mess_listview.setVisibility(View.GONE);
						no_mess.setVisibility(View.VISIBLE);
					}

					try {
						stopLoad();
					} catch (Exception e) {
					}

				}
				if (entity.getEventType() == UserPresenter.sysMess_fail) {
					ByAlert.alert(entity.getData());

					try {
						stopLoad();
					} catch (Exception e) {
					}
				}
			}
		}
	}

	@Override
	public void onRefresh() {
		pageNo = 1;
		userPresenter = new UserPresenter(this);
		userPresenter.getSystemMess(uid, "1", pageNo + "", "10");
	}

	@Override
	public void onLoadMore() {
		pageNo++;
		userPresenter = new UserPresenter(this);
		userPresenter.getSystemMess(uid, "1", pageNo + "", "10");
	}

	/**
	 * 停止加载
	 */
	public void stopLoad() {
		mess_listview.stopLoadMore();
		mess_listview.stopRefresh();
	}
}
